package org.study.controller;

import dev.langchain4j.data.message.ImageContent;
import dev.langchain4j.data.message.TextContent;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.util.Base64;

/**
 * @author: jzhou
 * @date: 2025/7/8-17:36
 * @version: 1.0
 * @description: TODO
 */
@RestController
@Slf4j
public class ImageModelController
{
    @jakarta.annotation.Resource(name = "qwen-vl")
    private ChatModel chatModel;

    @Value("classpath:static/images/img.png")
    private Resource resource;//import org.springframework.core.io.Resource;

    /**
     * @Description: 通过Base64编码将图片转化为字符串，结合ImageContent和TextContent 一起发送到模型进行处理。
     *测试地址：http://localhost:8080/image/call
     */
    @GetMapping(value = "/image/call")
    public String readImageContent() throws IOException
    {

        byte[] byteArray = resource.getContentAsByteArray();
        String base64Data = Base64.getEncoder().encodeToString(byteArray);

        UserMessage userMessage = UserMessage.from(
                TextContent.from("从以下图片中获取柱状图展示的对比信息"),
                ImageContent.from(base64Data,"image/png")
        );

        ChatResponse chatResponse = chatModel.chat(userMessage);
        String result = chatResponse.aiMessage().text();

        System.out.println(result);

        return result;
    }
}
